package core

import (
	"fmt"
	"io/ioutil"
	"os"

	yaml "gopkg.in/yaml.v2"
)

type Conf struct {
	Http     Http     `yaml:"http"`
	Database Database `yaml:"database"`
	Redis    Redis    `yaml:"redis"`
	Log      Log      `yaml:"log"`
}

type Http struct {
	Host string `yaml:"host"`
	Port int    `yaml:"port"`
}

type Database struct {
	Type     string `yaml:"type"`
	Host     string `yaml:"host"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	Dbname   string `yaml:"dbname"`
	Conf     string `yaml:"conf"`
}

type Redis struct {
	Enable bool `yaml:"enable"`
}

type Log struct {
	Output string `yaml:"output"`
	Level  string `yaml:"level"`
}

func InitConfig() {
	CONF = new(Conf)

	content, err := ioutil.ReadFile("config.yml")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	err = yaml.Unmarshal(content, CONF)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
